package com.lovelc.cn;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;


/**
 * @ClassName HbaseClinet
 * @Description TODO
 * @Author huangjh5
 * @Date 2021/1/5 13:01
 */
public class HbaseClinet {

    public static void main(String[] args) throws IOException {
        //获取hbase连接
        Configuration conf =  HBaseConfiguration.create();

        conf.set("hbase.zookeeper.quorum","linux121,linux122");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        Connection conn = ConnectionFactory.createConnection(conf);

        //创建表 create user,friends
        Admin admin = conn.getAdmin();
        if (!admin.tableExists(TableName.valueOf("user"))) {
            //进行描述
            HTableDescriptor user = new HTableDescriptor(TableName.valueOf("user"));
            user.addFamily(new HColumnDescriptor("friends"));
            admin.createTable(user);
        }

        //插入数据
        Table user = conn.getTable(TableName.valueOf("user"));
        Put put = new Put(Bytes.toBytes("user_name_1"));
        put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("user_name_2"), Bytes.toBytes("user_name_2"));
        put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("user_name_3"), Bytes.toBytes("user_name_3"));
        user.put(put);

        put = new Put(Bytes.toBytes("user_name_2"));
        put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("user_name_1"), Bytes.toBytes("user_name_1"));
        put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("user_name_3"), Bytes.toBytes("user_name_3"));
        user.put(put);

        put = new Put(Bytes.toBytes("user_name_3"));
        put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("user_name_2"), Bytes.toBytes("user_name_2"));
        put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("user_name_1"), Bytes.toBytes("user_name_1"));
        user.put(put);


    }

}
